<?php
/**
 * Created by PhpStorm.
 * User:lyq
 * Date: 2018/11/24
 * Time: 16:51
 */

namespace app\admin\controller;

use think\Exception;
use think\Db;
use app\common\model\Message;

class Advert extends Common
{
    public function index()
    {
        //标题
        $this->templateTitle("广告模块");

        $where = [];

        $id = trim(input('id'));
        $this->assign('id', $id);
        if ($id) {
            $where['ad.id'] = intval($id);
        }
        if (!empty($app_shop_id)) {
            $where['ad.app_shop_id'] = $app_shop_id;
        }

        $type = trim(input('type'));
        $this->assign('type', $type);
        if ($type) {
            $where['ad.type'] = $type;
        } else {
            $where['ad.type'] = ['gt', 0];
        }


        $is_visible = trim(input('is_visible'));
        $this->assign('is_visible', $is_visible);
        if ($is_visible != '') {
            $where['ad.is_visible'] = $is_visible;
        }

        //获取所有用户
        $list = Db::name("advert")
            ->alias('ad')
            ->where($where)
            ->order("ad.display_order DESC,ad.id DESC")
            ->paginate(10);

        $this->assign("list", $list);
        $this->assign('arr', config('link_type'));
        return $this->fetch();
    }

    //添加
    public function add()
    {
        $link_type = config('link_type');
        if (request()->isGet()) {
            $this->layoutModal();
            $this->templateTitle('添加广告');
            return $this->fetch();
        }

        if (request()->isAjax()) {
            //创建数据
            $data = array(
                "create_time" => now_datetime(),
                "update_time" => now_datetime()
            );

            //广告图片
            $data["img_url"] = request()->param("img_url");
            if (empty($data['img_url'])) {
                return $this->ajaxError('请上传广告图片');
            }

            $data["description"] = trim(request()->param("description"));

            $data['type'] = intval(request()->param('type'));
            if (empty($data['type'])) {
                return $this->ajaxError('请选择跳转类型');
            }

            $data['link_url'] = str_replace(' ', '', request()->param('link_url'));
            if ($link_type[$data['type']]['link_need'] == 1 && $data['link_url'] == '') {
                return $this->ajaxError($link_type[$data['type']]['placeholder']);
            }

            if ($data['type'] == 1) {
                $check = is_personal($data['link_url']);
                if ($check['status'] == 1) {
                    return $this->ajaxError($check['info']);
                }
            }

            //排序
            $data["display_order"] = request()->param("display_order");
            //是否显示
            $data["is_visible"] = request()->param("visible");
            try {
                $result = Db::name('advert')
                    ->insert($data);
                if ($result) {
                    /*操作记录*/
                    Message::admin_log(0, "后台添加广告");
                    return $this->ajaxSuccess("添加成功！");
                } else {
                    $this->ajaxError('添加失败');
                }
            } catch (Exception $exception) {
                return $this->ajaxError($exception->getMessage());
            }
        }
    }

    //编辑
    public function edit()
    {
        //ID
        $Id = request()->param("id");
        $link_type = config('link_type');
        if (request()->isGet()) {
            $this->layoutModal();

            //获取信息
            $info = Db::name("advert")
                ->where("id", $Id)
                ->find();
            $this->assign("info", $info);

            $this->assign("single_image", array("img_url" => $info["img_url"]));
            $this->assign("type", $info['type']);
            $this->assign("link_url", $info['link_url']);
            return $this->fetch();
        }

        if (request()->isAjax()) {
            //创建数据
            $data = array(
                "update_time" => now_datetime(),
            );
            //广告图片
            $data["img_url"] = request()->param("img_url");
            if (empty($data['img_url'])) {
                return $this->ajaxError('请上传广告图片');
            }

            $data["description"] = trim(request()->param("description"));

            $data['type'] = intval(request()->param('type'));
            if (empty($data['type'])) {
                return $this->ajaxError('请选择跳转类型');
            }

            $data['link_url'] = str_replace(' ', '', request()->param('link_url'));
            if ($link_type[$data['type']]['link_need'] == 1 && $data['link_url'] == '') {
                return $this->ajaxError($link_type[$data['type']]['placeholder']);
            }
            if ($data['type'] == 1) {
                $check = is_personal($data['link_url']);
                if ($check['status'] == 1) {
                    return $this->ajaxError($check['info']);
                }
            }
            //排序
            $data["display_order"] = request()->param("display_order");
            //是否显示
            $data["is_visible"] = request()->param("visible");

            try {
                //更新分类ID路径
                Db::name("advert")
                    ->where("id", $Id)
                    ->update($data);
                /*操作记录*/
                Message::admin_log(0, "后台修改广告" . $Id);
                return $this->ajaxSuccess("更新成功！");
            } catch (Exception $exception) {
                $this->ajaxError($exception->getMessage());
            }
        }
    }

    //删除
    public function remove()
    {
        if (request()->isAjax()) {
            //获取ID
            $Id = request()->param("id");
            // 删除
            $result = Db::name('advert')
                ->where('id', $Id)
                ->delete();
            if ($result) {
                /*操作记录*/
                Message::admin_log(0, "后台删除广告");
                return $this->ajaxSuccess("删除成功！");
            } else {
                return $this->ajaxError("删除失败！");
            }

        }
    }

    //更改菜单显示隐藏
    public function visible()
    {
        if (request()->isAjax()) {
            //菜单ID
            $id = request()->param("id");
            //获取可见性状态
            $status = request()->param("status");

            try {
                //修改菜单显示隐藏
                $save = Db::name("advert")
                    ->where("id", $id)
                    ->setField("is_visible", $status);

                if ($save) {
                    /*操作记录*/
                    Message::admin_log(0, "后台修改广告的状态");
                    return $this->ajaxSuccess("设置广告显示或隐藏成功！");
                } else {
                    return $this->ajaxError("设置广告显示或隐藏失败！");
                }
            } catch (Exception $exception) {
                return $this->ajaxError($exception->getMessage());
            }
        }
    }
}